package it.rap.application.logic;

import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BusinnessLogicAsp {
	
	Logger logger = Logger.getLogger( this.getClass().getSimpleName() );
	//Logger logger = new Logger();
	
	@Before(  "execution(public * it.rap.application.logic.mappers.*.remove(..)) ||"
			+ " execution(public * it.rap.application.logic.mappers.*.refresh(..)) ||"
			+ "execution(public * it.rap.application.logic.mappers.*.persist(..)) ||"
			+ "execution(public * it.rap.application.logic.mappers.*.merge(..)) ||"
			+ "execution(public * it.rap.application.logic.mappers.*.find(..))")
	public void beforeMethodRmove(JoinPoint jp) {		
		logger.info("#S# before " + jp.getSignature().getName());
		BusinnessLogic bs = (BusinnessLogic) jp.getTarget();
		if ( bs != null ){
			bs.openTransaction();
		}
		logger.info("#E# before " + jp.getSignature().getName());
	}
	
	@After(  "execution(public * it.rap.application.logic.mappers.*.remove(..)) ||"
			+ " execution(public * it.rap.application.logic.mappers.*.refresh(..)) ||"
			+ " execution(public * it.rap.application.logic.mappers.*.persist(..))||"
			+ "execution(public * it.rap.application.logic.mappers.*.merge(..)) ||"
			+ "execution(public * it.rap.application.logic.mappers.*.find(..))")
	public void afterMethodRemove(JoinPoint jp) {
		logger.info("#S# After " + jp.getSignature().getName());
		BusinnessLogic bs = (BusinnessLogic) jp.getTarget();
		if ( bs != null ){
			bs.commit();
		}
	    logger.info("#E# After " + jp.getSignature().getName());
	}
}
